package hu.tortavar2.client.view.adminpages;

import hu.tortavar2.client.AdminService;
import hu.tortavar2.client.AdminServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class AdminMainPage_AddImage extends Composite {

	AdminServiceAsync adminService = GWT.create(AdminService.class);
	
	private static AdminMainPage_AddImageUiBinder uiBinder = GWT
			.create(AdminMainPage_AddImageUiBinder.class);

	interface AdminMainPage_AddImageUiBinder extends
			UiBinder<Widget, AdminMainPage_AddImage> {
	}

	public AdminMainPage_AddImage() {
		initWidget(uiBinder.createAndBindUi(this));
		setFields();
	}

	@UiField Anchor addNewImageButton;
	@UiField Grid table;
	@UiField FormPanel addNewImageForm;
	@UiField HTMLPanel grid_02_left;
	@UiField HTMLPanel padding_color_left;
	@UiField ListBox imageCategoryListBox;
	@UiField ListBox imageTypeListBox;
	
	private void setFields() {
		addNewImageButton.getElement().setClassName("button");
		addNewImageForm.setWidget(table);
		
		addNewImageForm.setMethod(FormPanel.METHOD_POST);
		addNewImageForm.setEncoding(FormPanel.ENCODING_MULTIPART);
		setBlobStoreImageUrl();
		
		grid_02_left.getElement().setClassName("grid_02");
		padding_color_left.getElement().setClassName("padding pink");
	}
	
	private void setBlobStoreImageUrl() {
		adminService.getBlobStoreImageUploadUrl(new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				addNewImageForm.setAction(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	@UiHandler("addNewImageButton")
	void onclickSubmit(ClickEvent e) {
		addNewImageForm.submit();
		addNewImageForm.reset();
	}
}
